//
//  HX_ViewController.m
//  UI27-Block练习-播放按钮
//
//  Created by 韩旭 on 16/8/19.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "HX_ViewController.h"
#import "UIView+Frame.h"

@interface HX_ViewController ()
@property (nonatomic, copy) void(^play)(UIButton *play);
@property (nonatomic, copy) void(^pause)(UIButton *pause);
@property (nonatomic, copy) void(^next)(UIButton *nextBtn);
@property (nonatomic, copy) void(^last)(UIButton *lastBtn);

@end

@implementation HX_ViewController
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}

- (void)play:(void (^)(UIButton *))play pause:(void (^)(UIButton *))pause {
    self.play = play;
    self.pause = pause;
}
- (void)nextBtn:(void (^)(UIButton *))nextBtn LastBtn:(void (^)(UIButton *))lastBtn {
    self.next = nextBtn;
    self.last = lastBtn;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setUp];
    }
    return self;
}
- (void)setUp
{
    self.playButton = [self buttonImageWithName:@"1"action:@selector(play:)];
    self.nextButton = [self buttonImageWithName:@"2"action:@selector(lastBtn:)];
    self.lastButton = [self buttonImageWithName:@"3" action:@selector(nextBtn:)];
    _playButton.backgroundColor = [UIColor greenColor];
    _nextButton.backgroundColor = [UIColor purpleColor];
    _lastButton.backgroundColor = [UIColor cyanColor];
    [self addSubview:_nextButton];
    [self addSubview:_lastButton];
    [self addSubview:_playButton];
    
}
- (void)nextBtn:(UIButton *)btn {
    self.next(btn);
}
- (void)lastBtn:(UIButton *)btn {
    self.last(btn);
}
- (void)play:(UIButton *)playBtn {
    [playBtn setImage:[UIImage imageNamed:@"4"] forState:UIControlStateNormal];
    [playBtn removeTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    [playBtn addTarget:self action:@selector(stop:) forControlEvents:UIControlEventTouchUpInside];
    self.play(playBtn);
}
- (void)stop:(UIButton *)btn {
    [btn setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
    [btn removeTarget:self action:@selector(stop:) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    self.pause(btn);
}
- (UIButton *)buttonImageWithName:(NSString *)imageName action:(SEL)action {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    return btn;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    self.playButton.frame = CGRectMake(0, 0, 100, 100);
    _playButton.center = self.center;
    self.nextButton.frame = CGRectMake(0, 0, 50, 50);
    _nextButton.centerX = self.bounds.size.width * 0.2;
    _nextButton.centerY = _playButton.center.y;
    self.lastButton.frame = CGRectMake(0, 0, 50, 50);
    _lastButton.centerX = self.frame.size.width * 0.8;
    _lastButton.centerY = _playButton.center.y;
    
}

@end